/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tuoc;

import java.sql.ResultSet;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author apple
 */
public class orders_model extends My_model{
    int id;
    int table_id;
    double total;
    String created_date;
    int modified_date;
    
    public Vector get_elements()
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM orders");
        return this.to_domain_models(rs);
    }
    
    public orders_model get_element_by_id(String id)
    {
        ResultSet rs = this.executeSqlString("SELECT * FROM orders WHERE id = " + id);
        return this.to_domain_model(rs);
    }
    
    public int insert_data(Map<String,String> data)
            
    {
        String sqlString = String.format("INSERT INTO orders VALUES(NULL,%s,%s,'%s',%s)",
                (data.get("table_id") != null) ? data.get("table_id") : "NULL",
                (data.get("total") != null) ? data.get("total") : "NULL",
                (data.get("created_date") != null) ? data.get("created_date") : "NULL",
                (data.get("modified_date") != null) ? data.get("modified_date") : "NULL"
                );
        return this.insertDatabase(sqlString);
    }
    
    
//    ----PROPERTIES
    
    public String get_table_name()
    {
        return new tables_model().get_element_by_id(this.table_id + "").name;
    }
    
    public double get_allTotal()
    {
        double result = 0;
        String sql = "SELECT SUM(total) AS sumTotal FROM orders";
        ResultSet rs = this.executeSqlString(sql);
        try {
            while(rs.next())
            {
                result = rs.getDouble("sumTotal");
            }    
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
//    ----MODEL
    
    public orders_model to_domain_model(ResultSet rs)
    {
        orders_model result = new orders_model();
        try {
            if(rs.next())
            {
                result.id = rs.getInt("id");
                result.table_id = rs.getInt("table_id");
                result.total = rs.getDouble("total");
                result.created_date = rs.getString("created_date");
                result.modified_date = rs.getInt("modified_date");
            }    
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public Vector to_domain_models(ResultSet rs)
    {
        Vector result = new Vector();
        try {
            while(rs.next())
            {
                orders_model item = new orders_model();
                item.id = rs.getInt("id");
                item.table_id = rs.getInt("table_id");
                item.total = rs.getDouble("total");
                item.created_date = rs.getString("created_date");
                item.modified_date = rs.getInt("modified_date");
                
                result.add(item);
            }
            
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
}
